package com.shop2cn.iapi.pretrade.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.pretrade.service.AdjustCall;
import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.enums.CancelOrderTypeEnum;
import com.shop2cn.iapi.sqoptrade.parameter.req.PlaceOrderReq;
import com.shop2cn.iapi.sqoptrade.parameter.vo.ActivityVo;
import com.shop2cn.iapi.sqoptrade.parameter.vo.OrderItemVo;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqPretradeService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.business.service.shenqi.SqTradingProductService;
import com.ymttest.database.sharding.db.ext.model.OrderAutoAcceptActivityDetailPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;
import com.ymttest.utils.EnvSetup;

/**
 * 自动确认收货提醒通知FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AutoAccept {

	private static PlaceOrderReq placeOrderReq = new PlaceOrderReq();
	
	private static AdjustCall autoAcceptCall;
	
	private static OrderWapper orderWapper = new OrderWapper();
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("活动自动确认接单");
	}

	@Before
	public void caseUp() {

		autoAcceptCall = new AdjustCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(autoAcceptCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDB(int orderStatus, Long orderId, Long sellerId, OrderItemVo orderItemVo, Boolean isAuto) {
		
		String catalogId = orderItemVo.getCatalogId();
		ActivityVo activityVo = orderItemVo.getActivity();
		
		Logger.comment("验证order_auto_accept_activity_detail");
		
		OrderAutoAcceptActivityDetailPo actualResult = orderWapper
				.selectAutoAcceptActivityDetailByOrderIdAndCatalogId(orderId,
						catalogId);
		
		Logger.verifyEquals(activityVo.getActivityId(),
				actualResult.getActivityId(), "验证ActivityId");
		Logger.verifyEquals(sellerId, actualResult.getSellerId(), "验证SellerId");

		if (orderStatus == 17 && isAuto)
			Logger.verifyEquals(1, actualResult.getAcceptOrder(),
					"验证AcceptOrder");
		else if (orderStatus == 1 || orderStatus == 2)
			Logger.verifyEquals(0, actualResult.getAcceptOrder(),
					"验证AcceptOrder");
		else
			Logger.verifyEquals(2, actualResult.getAcceptOrder(),
					"验证AcceptOrder");

		Logger.comment("验证order");

		Logger.verifyEquals(orderStatus, orderShardingWapper
				.selectOrderbyOrderId(orderId).getOrderStatus(),
				"验证OrderStatus");
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AutoAccept_001() {
		Logger.start(true, "订单已支付，活动结束后自动接单");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			Integer activityId = SqTradingProductService.agentProductAddActivity(productBean, false);
			
			placeOrderReq = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(4321, BigDecimal.ZERO, 20238699,
							productBean, (int) autotest_goodsOwnerId);
			
			OrderItemVo orderItemVo = placeOrderReq.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			ActivityVo activityVo = new ActivityVo();
			
			activityVo.setActivityId(activityId);
			activityVo.setActivityTemplateId(5123);
			activityVo.setAfterActivityFinishAccept(true);
			activityVo.setProductInActivityId(1234);
			
			orderItemVo.setActivity(activityVo);

			TradingServiceResp placeOrderResp = SqTradingCallService.callPlaceOrderService(placeOrderReq);

			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			Long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			Thread.sleep(3000);
			
			// 活动结束通知
			SqPretradeService.onFinish((long) activityId);

			// 发送请求
			autoAcceptCall.setData("{}");
			autoAcceptCall.callService();
			Logger.verifyEquals("ok", autoAcceptCall.getReturnData(),
					"验证接口返回");
			
			verifyDB(17, orderId, sellerId, orderItemVo, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AutoAccept_002() {
		Logger.start(true, "订单已接单，活动结束后无需自动接单");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			Integer activityId = SqTradingProductService.agentProductAddActivity(productBean, false);
			
			placeOrderReq = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(4321, BigDecimal.ZERO, 20238699,
							productBean, (int) autotest_goodsOwnerId);
			
			OrderItemVo orderItemVo = placeOrderReq.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			ActivityVo activityVo = new ActivityVo();
			
			activityVo.setActivityId(activityId);
			activityVo.setActivityTemplateId(5123);
			activityVo.setAfterActivityFinishAccept(true);
			activityVo.setProductInActivityId(1234);
			
			orderItemVo.setActivity(activityVo);

			TradingServiceResp placeOrderResp = SqTradingCallService.callPlaceOrderService(placeOrderReq);

			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			Long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			Thread.sleep(5000);
			
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);
			
			// 活动结束通知
			SqPretradeService.onFinish((long) activityId);
			
			// 发送请求
			autoAcceptCall.setData("{}");
			autoAcceptCall.callService();
			Logger.verifyEquals("ok", autoAcceptCall.getReturnData(),
					"验证接口返回");
			
			verifyDB(17, orderId, sellerId, orderItemVo, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AutoAccept_003() {
		Logger.start(true, "订单已发货，活动结束后无需自动接单");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			Integer activityId = SqTradingProductService.agentProductAddActivity(productBean, false);
			
			placeOrderReq = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(4321, BigDecimal.ZERO, 20238699,
							productBean, (int) autotest_goodsOwnerId);
			
			OrderItemVo orderItemVo = placeOrderReq.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			ActivityVo activityVo = new ActivityVo();
			
			activityVo.setActivityId(activityId);
			activityVo.setActivityTemplateId(5123);
			activityVo.setAfterActivityFinishAccept(true);
			activityVo.setProductInActivityId(1234);
			
			orderItemVo.setActivity(activityVo);

			TradingServiceResp placeOrderResp = SqTradingCallService.callPlaceOrderService(placeOrderReq);

			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			Long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			Thread.sleep(5000);
			
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);
			
			SqTradingCallService.delivery(orderId);
			
			// 活动结束通知
			SqPretradeService.onFinish((long) activityId);
			
			// 发送请求
			autoAcceptCall.setData("{}");
			autoAcceptCall.callService();
			Logger.verifyEquals("ok", autoAcceptCall.getReturnData(),
					"验证接口返回");
			
			verifyDB(3, orderId, sellerId, orderItemVo, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AutoAccept_004() {
		Logger.start(true, "订单已取消，活动结束后无需自动接单");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			Integer activityId = SqTradingProductService.agentProductAddActivity(productBean, false);
			
			placeOrderReq = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(4321, BigDecimal.ZERO, 20238699,
							productBean, (int) autotest_goodsOwnerId);
			
			OrderItemVo orderItemVo = placeOrderReq.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			ActivityVo activityVo = new ActivityVo();
			
			activityVo.setActivityId(activityId);
			activityVo.setActivityTemplateId(5123);
			activityVo.setAfterActivityFinishAccept(true);
			activityVo.setProductInActivityId(1234);
			
			orderItemVo.setActivity(activityVo);

			TradingServiceResp placeOrderResp = SqTradingCallService.callPlaceOrderService(placeOrderReq);

			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			Long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			Thread.sleep(5000);
			
			SqTradingCallService.cancelOrder(orderId, CancelOrderTypeEnum.SELLER_CANCEL_ORDER, autotest_goodsOwnerId);
			
			// 活动结束通知
			SqPretradeService.onFinish((long) activityId);
			
			// 发送请求
			autoAcceptCall.setData("{}");
			autoAcceptCall.callService();
			Logger.verifyEquals("ok", autoAcceptCall.getReturnData(),
					"验证接口返回");
			
			verifyDB(13, orderId, sellerId, orderItemVo, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AutoAccept_005() {
		Logger.start(true, "订单已下单，活动结束后无需自动接单");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			Integer activityId = SqTradingProductService.agentProductAddActivity(productBean, false);
			
			placeOrderReq = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(4321, BigDecimal.ZERO, 20238699,
							productBean, (int) autotest_goodsOwnerId);
			
			OrderItemVo orderItemVo = placeOrderReq.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			ActivityVo activityVo = new ActivityVo();
			
			activityVo.setActivityId(activityId);
			activityVo.setActivityTemplateId(5123);
			activityVo.setAfterActivityFinishAccept(true);
			activityVo.setProductInActivityId(1234);
			
			orderItemVo.setActivity(activityVo);

			TradingServiceResp placeOrderResp = SqTradingCallService.callPlaceOrderService(placeOrderReq);

			Long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			
			// 活动结束通知
			SqPretradeService.onFinish((long) activityId);
			
			// 发送请求
			autoAcceptCall.setData("{}");
			autoAcceptCall.callService();
			Logger.verifyEquals("ok", autoAcceptCall.getReturnData(),
					"验证接口返回");
			
			verifyDB(1, orderId, sellerId, orderItemVo, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AutoAccept_006() {
		Logger.start(true, "同一个订单多个商品，参加多个活动，订单已支付，其中一个活动结束，活动结束后不能自动接单");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			Integer activityId = SqTradingProductService.agentProductAddActivity(productBean, false);
			
			placeOrderReq = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(4321, BigDecimal.ZERO, 20238699,
							productBean, (int) autotest_goodsOwnerId);
			
			OrderItemVo orderItemVo = placeOrderReq.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			ActivityVo activityVo = new ActivityVo();
			
			activityVo.setActivityId(activityId);
			activityVo.setActivityTemplateId(5123);
			activityVo.setAfterActivityFinishAccept(true);
			activityVo.setProductInActivityId(1234);
			
			orderItemVo.setActivity(activityVo);
			
			AgentProductBean productBean2 = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			
			OrderItemVo orderItemVo2 = SqTradingCallService.createOrderItemVo(
					productBean2.getProductId(), productBean2
							.getCatalogList().get(0));
			
			placeOrderReq.getOrderGroups().get(0).getOrders().get(0)
					.getOrderItems().add(orderItemVo2);
			
			Integer activityId2 = SqTradingProductService.agentProductAddActivity(productBean2, false);
			
			ActivityVo activityVo2 = new ActivityVo();
			
			activityVo2.setActivityId(activityId2);
			activityVo2.setActivityTemplateId(5123);
			activityVo2.setAfterActivityFinishAccept(true);
			activityVo2.setProductInActivityId(1234);
			
			orderItemVo2.setActivity(activityVo2);

			TradingServiceResp placeOrderResp = SqTradingCallService.callPlaceOrderService(placeOrderReq);

			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			Long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			Thread.sleep(3000);
			
			// 活动结束通知
			SqPretradeService.onFinish((long) activityId);

			// 发送请求
			autoAcceptCall.setData("{}");
			autoAcceptCall.callService();
			Logger.verifyEquals("ok", autoAcceptCall.getReturnData(),
					"验证接口返回");
			
			verifyDB(2, orderId, sellerId, orderItemVo, false);
			verifyDB(2, orderId, sellerId, orderItemVo2, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AutoAccept_007() {
		Logger.start(true, "同一个订单多个商品，一个活动商品，订单已支付，活动结束后自动接单");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			Integer activityId = SqTradingProductService.agentProductAddActivity(productBean, false);
			
			placeOrderReq = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(4321, BigDecimal.ZERO, 20238699,
							productBean, (int) autotest_goodsOwnerId);
			
			OrderItemVo orderItemVo = placeOrderReq.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			ActivityVo activityVo = new ActivityVo();
			
			activityVo.setActivityId(activityId);
			activityVo.setActivityTemplateId(5123);
			activityVo.setAfterActivityFinishAccept(true);
			activityVo.setProductInActivityId(1234);
			
			orderItemVo.setActivity(activityVo);
			
			AgentProductBean productBean2 = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			
			OrderItemVo orderItemVo2 = SqTradingCallService.createOrderItemVo(
					productBean2.getProductId(), productBean2
							.getCatalogList().get(0));
			
			placeOrderReq.getOrderGroups().get(0).getOrders().get(0)
					.getOrderItems().add(orderItemVo2);

			TradingServiceResp placeOrderResp = SqTradingCallService.callPlaceOrderService(placeOrderReq);

			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			Long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			Thread.sleep(3000);
			
			// 活动结束通知
			SqPretradeService.onFinish((long) activityId);

			// 发送请求
			autoAcceptCall.setData("{}");
			autoAcceptCall.callService();
			Logger.verifyEquals("ok", autoAcceptCall.getReturnData(),
					"验证接口返回");
			
			verifyDB(17, orderId, sellerId, orderItemVo, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AutoAccept_008() {
		Logger.start(true, "同一个订单多个商品，参加多个活动，订单已支付，所有活动结束，活动结束后自动接单");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			Integer activityId = SqTradingProductService.agentProductAddActivity(productBean, false);
			
			placeOrderReq = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(4321, BigDecimal.ZERO, 20238699,
							productBean, (int) autotest_goodsOwnerId);
			
			OrderItemVo orderItemVo = placeOrderReq.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			ActivityVo activityVo = new ActivityVo();
			
			activityVo.setActivityId(activityId);
			activityVo.setActivityTemplateId(5123);
			activityVo.setAfterActivityFinishAccept(true);
			activityVo.setProductInActivityId(1234);
			
			orderItemVo.setActivity(activityVo);
			
			AgentProductBean productBean2 = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			
			OrderItemVo orderItemVo2 = SqTradingCallService.createOrderItemVo(
					productBean2.getProductId(), productBean2
							.getCatalogList().get(0));
			
			placeOrderReq.getOrderGroups().get(0).getOrders().get(0)
					.getOrderItems().add(orderItemVo2);
			
			Integer activityId2 = SqTradingProductService.agentProductAddActivity(productBean2, false);
			
			ActivityVo activityVo2 = new ActivityVo();
			
			activityVo2.setActivityId(activityId2);
			activityVo2.setActivityTemplateId(5123);
			activityVo2.setAfterActivityFinishAccept(true);
			activityVo2.setProductInActivityId(1234);
			
			orderItemVo2.setActivity(activityVo2);

			TradingServiceResp placeOrderResp = SqTradingCallService.callPlaceOrderService(placeOrderReq);

			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			Long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			Thread.sleep(3000);
			
			// 活动结束通知
			SqPretradeService.onFinish((long) activityId);
			SqPretradeService.onFinish((long) activityId2);

			// 发送请求
			autoAcceptCall.setData("{}");
			autoAcceptCall.callService();
			Logger.verifyEquals("ok", autoAcceptCall.getReturnData(),
					"验证接口返回");
			
			verifyDB(17, orderId, sellerId, orderItemVo, true);
			verifyDB(17, orderId, sellerId, orderItemVo2, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AutoAccept_009() {
		Logger.start(true, "同一个订单多个商品，参加一个活动，订单已支付，活动结束，活动结束后自动接单");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			Integer activityId = SqTradingProductService.agentProductAddActivity(productBean, false);
			
			placeOrderReq = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(4321, BigDecimal.ZERO, 20238699,
							productBean, (int) autotest_goodsOwnerId);
			
			OrderItemVo orderItemVo = placeOrderReq.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			ActivityVo activityVo = new ActivityVo();
			
			activityVo.setActivityId(activityId);
			activityVo.setActivityTemplateId(5123);
			activityVo.setAfterActivityFinishAccept(true);
			activityVo.setProductInActivityId(1234);
			
			orderItemVo.setActivity(activityVo);
			
			OrderItemVo orderItemVo2 = SqTradingCallService.createOrderItemVo(
					productBean.getProductId(), productBean
							.getCatalogList().get(1));
			
			placeOrderReq.getOrderGroups().get(0).getOrders().get(0)
					.getOrderItems().add(orderItemVo2);
			
			ActivityVo activityVo2 = new ActivityVo();
			
			activityVo2.setActivityId(activityId);
			activityVo2.setActivityTemplateId(5123);
			activityVo2.setAfterActivityFinishAccept(true);
			activityVo2.setProductInActivityId(1234);
			
			orderItemVo2.setActivity(activityVo2);

			TradingServiceResp placeOrderResp = SqTradingCallService.callPlaceOrderService(placeOrderReq);

			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			Long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			Thread.sleep(3000);
			
			// 活动结束通知
			SqPretradeService.onFinish((long) activityId);

			// 发送请求
			autoAcceptCall.setData("{}");
			autoAcceptCall.callService();
			Logger.verifyEquals("ok", autoAcceptCall.getReturnData(),
					"验证接口返回");
			
			verifyDB(17, orderId, sellerId, orderItemVo, true);
			verifyDB(17, orderId, sellerId, orderItemVo2, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AutoAccept_010() {
		Logger.start(true, "同一个订单多个商品，参加多个活动，其中一个活动非自动接单，订单已支付，自动接单活动结束，活动结束后自动接单");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			Integer activityId = SqTradingProductService.agentProductAddActivity(productBean, false);
			
			placeOrderReq = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(4321, BigDecimal.ZERO, 20238699,
							productBean, (int) autotest_goodsOwnerId);
			
			OrderItemVo orderItemVo = placeOrderReq.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			ActivityVo activityVo = new ActivityVo();
			
			activityVo.setActivityId(activityId);
			activityVo.setActivityTemplateId(5123);
			activityVo.setAfterActivityFinishAccept(true);
			activityVo.setProductInActivityId(1234);
			
			orderItemVo.setActivity(activityVo);
			
			AgentProductBean productBean2 = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			
			OrderItemVo orderItemVo2 = SqTradingCallService.createOrderItemVo(
					productBean2.getProductId(), productBean2
							.getCatalogList().get(0));
			
			placeOrderReq.getOrderGroups().get(0).getOrders().get(0)
					.getOrderItems().add(orderItemVo2);
			
			Integer activityId2 = SqTradingProductService.agentProductAddActivity(productBean2, false);
			
			ActivityVo activityVo2 = new ActivityVo();
			
			activityVo2.setActivityId(activityId2);
			activityVo2.setActivityTemplateId(5123);
			activityVo2.setAfterActivityFinishAccept(false);
			activityVo2.setProductInActivityId(1234);
			
			orderItemVo2.setActivity(activityVo2);

			TradingServiceResp placeOrderResp = SqTradingCallService.callPlaceOrderService(placeOrderReq);

			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			Long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			Thread.sleep(3000);
			
			// 活动结束通知
			SqPretradeService.onFinish((long) activityId);
			SqPretradeService.onFinish((long) activityId2);

			// 发送请求
			autoAcceptCall.setData("{}");
			autoAcceptCall.callService();
			Logger.verifyEquals("ok", autoAcceptCall.getReturnData(),
					"验证接口返回");
			
			verifyDB(17, orderId, sellerId, orderItemVo, true);
//			verifyDB(17, orderId, sellerId, orderItemVo2, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
